package com.ruoyi.framework.aspectj;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * @Author li long
 * @Date 2022/9/24
 * @Version V1.0.0
 * @Description
 */
@Component
@Aspect
@Slf4j
class AutoSetDefaultParamsAspect {
    @SneakyThrows
    @Before("@annotation(com.ruoyi.common.annotation.AutoSetDefaultParams)")
    public void beforeEvent(JoinPoint point) {

        boolean isUpdateMethod=false;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        if (Objects.nonNull(request)) {
            String requestUrl = request.getRequestURL().toString();
            String reg = ".*update.*";
            isUpdateMethod= requestUrl.matches(reg);
        }

        SysUser sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();
        if(userId == null){
            userId = 0L;
        }

        //获得Controller方法的参数
        Object[] args = point.getArgs();
        //变量参数
        for(int i=0;i<args.length;i++){
            //对于参数，通常是实体对象，例如SignatureEntity
            Object entity = args[i];
            //获得参数类型,SignatureEntity.class
            Class<?> classes = entity.getClass();


            Method method;

            //当前进行的是新增操作，需要设置创建人createUser和创建时间createTime


            if(isUpdateMethod){
                method = getMethod(classes, "setUpdateBy", String.class);
                if (method != null) {
                    method.invoke(entity, userId.toString());
                }

                method = getMethod(classes, "setUpdateTime", Date.class);
                if (method != null) {
                    method.invoke(entity, new Date());
                }
                continue;
            }

             method = getMethod(classes, "setCreateBy", String.class);
            if (method != null) {
                method.invoke(entity, userId.toString());
            }
            method = getMethod(classes, "setCreateTime", Date.class);
            if (method != null) {
                method.invoke(entity, new Date());
            }




        }

        System.out.println(point);
    }

    /**
     * 获得方法对象
     * @param classes
     * @param name
     * @param types
     * @return
     */
    private Method getMethod(Class classes, String name, Class... types) {
        try {
            return classes.getMethod(name, types);
        } catch (NoSuchMethodException e) {
            return null;
        }
    }
}

